package cn.edu.djtu.fw.action;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Properties;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import cn.edu.djtu.fw.model.Car;
import cn.edu.djtu.fw.model.Goods;
import cn.edu.djtu.fw.model.Lost;
import cn.edu.djtu.fw.model.News;
import cn.edu.djtu.fw.service.CarServiceI;
import cn.edu.djtu.fw.service.GoodsServiceI;
import cn.edu.djtu.fw.service.LostServiceI;
import cn.edu.djtu.fw.service.NewsServiceI;
import cn.edu.djtu.fw.service.SystemServiceI;

@Component
public class InitAction implements ServletContextListener, ApplicationContextAware {
	private static ApplicationContext applicationContext;

	@SuppressWarnings("static-access")
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext = applicationContext;
	}

	@Override
	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		ServletContext application = servletContextEvent.getServletContext();
		Properties prop = new Properties();
		InputStream in = InitAction.class.getClassLoader().getResourceAsStream("data.properties");
		try {
			prop.load(in);
			String str = (String) application.getAttribute("webhitcount");
			in.close();
			File file = new File(InitAction.class.getClassLoader().getResource("").getPath(), "data.properties");
			OutputStream fos = new FileOutputStream(file);
			prop.setProperty("webhitcount", str);
			prop.store(fos, "the web application has been hited " + str + " times");
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void contextInitialized(ServletContextEvent servletContextEvent) {
		ServletContext application = servletContextEvent.getServletContext();
		NewsServiceI newsService = (NewsServiceI) applicationContext.getBean("newsService");
		List<News> index_news = (List<News>) newsService.getAllNews(1, 10, null).get("rows");
		application.setAttribute("index_news", index_news);

		LostServiceI lostService = (LostServiceI) applicationContext.getBean("lostService");
		List<Lost> index_lost = (List<Lost>) lostService.getAllLost(1, 10, null).get("rows");
		application.setAttribute("index_lost", index_lost);

		// Lost lost = new Lost();
		// List<Lost> s_lost = (List<Lost>) lostService.getAllLost(1, 10, lost).get("rows");
		// application.setAttribute("s_lost", s_lost);
		GoodsServiceI goodsService = (GoodsServiceI) applicationContext.getBean("goodsService");
		List<Goods> index_goods = (List<Goods>) goodsService.getAllGoods(1, 20, null).get("rows");
		application.setAttribute("index_goods", index_goods);

		// Goods goods = new Goods();
		// List<Goods> s_goods = (List<Goods>) goodsService.getAllGoods(1, 5, goods).get("rows");
		// application.setAttribute("s_goods", s_goods);
		CarServiceI carService = (CarServiceI) applicationContext.getBean("carService");
		List<Car> index_car = (List<Car>) carService.getAllCar(1, 20, null).get("rows");
		application.setAttribute("index_car", index_car);
		// 加载字典项
		SystemServiceI systemService = (SystemServiceI) applicationContext.getBean("systemService");
		List<String> keynames = systemService.getParmDicKeyName();
		for (String string : keynames) {
			application.setAttribute(string, systemService.initParmDic(string));
		}
		// Car car = new Car();
		// List<Car> s_car = (List<Car>) carService.getAllCar(1, 15, car).get("rows");
		// application.setAttribute("s_car", s_car);
		Properties prop = new Properties();
		InputStream in = InitAction.class.getClassLoader().getResourceAsStream("data.properties");
		try {
			prop.load(in);
			String str = prop.getProperty("webhitcount").trim();
			if (str != null && str.length() > 0) {
				application.setAttribute("webhitcount", str);
			}else {
				application.setAttribute("webhitcount", 1);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}